using DotNetty.Buffers;
using QQ.Client;
using QQ.Codecs;
using QQ.Utils;
using static QQ.Codecs.DotNettyByteBufferExtensions;
namespace QQ.Packet.Request
{
    public class OICQRequest : IRequest
    {
        private readonly QQDeviceConfig config;
        private readonly ECDHHelper ecdhHelper;
        public OICQRequest(QQDeviceConfig config, ECDHHelper ecdhHelper)
        {
            this.config = config;
            this.ecdhHelper = ecdhHelper;
        }
        public int uin { get; set; }
        public int cmd { get; set; }
        private IByteBuffer plainBody;
        public IByteBuffer PlainBody
        {
            get => plainBody;
            set
            {
                plainBody = Unpooled.Buffer()
                    .WriteByte(1)
                    .WriteByte(1)
                    .WriteBytes(config.RandomKey)
                    .WriteShort(258)
                    .WriteBytesWithLength(ecdhHelper.getPublicKey().ToByteBuffer(), LengthType.SHORT)
                    .WriteBytes(QQTea.Encrypt(value.ToArray(), ecdhHelper.getSharedKey()));
            }
        }
        public byte encryptID { get; set; } = 7;
        private static short unknowConst0x1 { get; set; } = 1;
        private static byte unknowConst0x3 { get; set; } = 3;
        private static byte const8_always_0 { get; set; } = 0;
        private static int unknowConst0x2 { get; set; } = 2;
        private static int constp_always_0 { get; set; } = 0;
        public IByteBuffer Serialize() => Unpooled.Buffer()
            .WriteByte(0x02)
            .WriteShort(27 + 2 + PlainBody.ReadableBytes)
            .WriteShort(config.protocolVersion)
            .WriteShort(cmd)
            .WriteShort(unknowConst0x1)
            .WriteInt(uin)
            .WriteByte(unknowConst0x3)
            .WriteByte(encryptID)
            .WriteByte(const8_always_0)
            .WriteInt(unknowConst0x2)
            .WriteInt(config.appClientVersion)
            .WriteInt(constp_always_0)
            .WriteBytes(PlainBody)
            .WriteByte(0x03);

    }
}